package net.huashitong.ssydt.provider.money.service;


import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.exceptions.ParamErrorServiceException;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.money.constant.MoneyConsts;
import net.huashitong.ssydt.money.entity.SsMoneyGoldBag;
import net.huashitong.ssydt.money.entity.vo.SsMoneyGoldBagQueryInVO;
import net.huashitong.ssydt.provider.money.mapper.SsMoneyGoldBagMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huiyang.yu
 */
@Service
@Transactional
public class SsMoneyGoldBagServiceImpl implements SsMoneyGoldBagService {

    private SsMoneyGoldBagMapper mapper;

    public SsMoneyGoldBagServiceImpl(SsMoneyGoldBagMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<SsMoneyGoldBag> queryByState() {
        return mapper.queryByState(SsMoneyGoldBag.State.SHOW);
    }

    @Override
    public SsMoneyGoldBag getById(Long goodsId) {
        return mapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public PaginationResult<SsMoneyGoldBag> paginationQuery(SsMoneyGoldBagQueryInVO ssMoneyGoldBagQueryInVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(ssMoneyGoldBagQueryInVO.getPageNumber());
        result.setPageSize(ssMoneyGoldBagQueryInVO.getPageSize());

        // 查询记录总数
        int total = mapper.queryCount(ssMoneyGoldBagQueryInVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsMoneyGoldBag> list = mapper.query(ssMoneyGoldBagQueryInVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public void insert(SsMoneyGoldBag ssMoneyGoldBag) {
        checkParameters(ssMoneyGoldBag);

        if (ssMoneyGoldBag.getGoldNumber() == null)
            throw new ParamErrorServiceException("金币数量不能为空");
        if (ssMoneyGoldBag.getNumberLimit() == null)
            ssMoneyGoldBag.setNumberLimit(0);
        if (ssMoneyGoldBag.getSequence() == null)
            ssMoneyGoldBag.setSequence(99);

        mapper.insert(ssMoneyGoldBag);
    }

    @Override
    public void updateById(SsMoneyGoldBag ssMoneyGoldBag) {
        checkParameters(ssMoneyGoldBag);

        // 判断记录是否存在
        SsMoneyGoldBag bag = mapper.selectByPrimaryKey(ssMoneyGoldBag.getId());
        if (bag == null) {
            throw new ServiceException("金币套餐不存在");
        }

        mapper.updateByPrimaryKeySelective(ssMoneyGoldBag);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    /**
     * 入参检查
     *
     * @param ssMoneyGoldBag 金币套餐对象
     */
    void checkParameters(SsMoneyGoldBag ssMoneyGoldBag) {
        switch (ssMoneyGoldBag.getNowExecute()) {
            case MoneyConsts.NowExecute.ORIGINA_PRICE:
            case MoneyConsts.NowExecute.FAVOURABLE_PRICE:
            case MoneyConsts.NowExecute.PREFERENCE_PRICE:
                break;
            default:
                throw new ParamErrorServiceException("当前执行价输入错误");
        }
        switch (ssMoneyGoldBag.getState()) {
            case SsMoneyGoldBag.State.SHOW:
            case SsMoneyGoldBag.State.HIDDEN:
                break;
            default:
                throw new ParamErrorServiceException("状态输入错误");
        }
    }
}
